# coding:utf-8
import re
from odoo import models, fields, api, _
from odoo.osv import expression


class PaulSaleOrderExtend(models.Model):
    _name = 'product.product'
    _inherit = 'product.product'

    permissible_proportion = fields.Float(u'允出比例%', related='product_tmpl_id.permissible_proportion', required=True)
    permissible_proportion_manager = fields.Boolean(u'管理允出比例', related='product_tmpl_id.permissible_proportion_manager')
    class_code1 = fields.Char('Class Code1')
    class_code2 = fields.Char('Class Code2')
    class_code3 = fields.Char('Class Code3')
    class_code4 = fields.Char('Class Code4')
    class_code5 = fields.Char('Class Code5')
    class_code6 = fields.Char('Class Code6')
    class_code7 = fields.Char('Class Code7')
    class_code8 = fields.Char('Class Code8')
    class_code9 = fields.Char('Class Code9')
    class_code10 = fields.Char('Class Code10')
    qc_flag = fields.Boolean(string=u'是否质检', related='product_tmpl_id.qc_flag')

    @api.onchange('permissible_proportion_manager')
    def onchange_proportion(self):
        if not self.permissible_proportion_manager:
            self.permissible_proportion = 0

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):
        if not args:
            args = []
        if name:
            positive_operators = ['=', 'ilike', '=ilike', 'like', '=like']
            products = self.env['product.product']
            if operator in positive_operators:
                products = self.search([('default_code', '=', name)] + args, limit=limit)
                if not products:
                    products = self.search([('barcode', '=', name)] + args, limit=limit)
            if not products and operator not in expression.NEGATIVE_TERM_OPERATORS:
                # Do not merge the 2 next lines into one single search, SQL search performance would be abysmal
                # on a database with thousands of matching products, due to the huge merge+unique needed for the
                # OR operator (and given the fact that the 'name' lookup results come from the ir.translation table
                # Performing a quick memory merge of ids in Python will give much better performance
                products = self.search(args + [('default_code', operator, name)], limit=limit)
                if not limit or len(products) < limit:
                    # we may underrun the limit because of dupes in the results, that's fine
                    limit2 = (limit - len(products)) if limit else False
                    products += self.search(args + [('id', 'not in', products.ids), '|', ('name', operator, name), ('attribute_value_ids', operator, name)], limit=limit2)
            elif not products and operator in expression.NEGATIVE_TERM_OPERATORS:
                products = self.search(args + ['&', '&', ('default_code', operator, name), ('name', operator, name), ('attribute_value_ids', operator, name)], limit=limit)
            if not products and operator in positive_operators:
                ptrn = re.compile('(\[(.*?)\])')
                res = ptrn.search(name)
                if res:
                    products = self.search([('default_code', '=', res.group(2))] + args, limit=limit)
            # still no results, partner in context: search on supplier info as last hope to find something
            if not products and self._context.get('partner_id'):
                suppliers = self.env['product.supplierinfo'].search([
                    ('name', '=', self._context.get('partner_id')),
                    '|',
                    ('product_code', operator, name),
                    ('product_name', operator, name)])
                if suppliers:
                    products = self.search([('product_tmpl_id.seller_ids', 'in', suppliers.ids)], limit=limit)
        else:
            products = self.search(args, limit=limit)
        return products.name_get()
